package com.iemes.controller.mould;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.iemes.annotation.SystemLog;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.DamagePartFormMap;
import com.iemes.entity.MouldCheckStandardFormMap;
import com.iemes.entity.UserFormMap;
import com.iemes.mapper.mould.DamagePartMapper;
import com.iemes.plugin.PageView;
import com.iemes.util.Common;

@Controller
@RequestMapping("/damagePart/")
public class DamagePartController extends BaseController {

	@Inject
	private DamagePartMapper damagePartMapper;
	
	@RequestMapping("damagePart_list")
	public String listUI(Model model) throws Exception {
		model.addAttribute("res", findByRes());
		return Common.BACKGROUND_PATH + "/mould/damagePart/damagePart_list";
	}
	
	@ResponseBody
	@RequestMapping("damagePart_findByPage")
	public PageView findByPage(String pageNow, String pageSize) throws Exception 
	{
		DamagePartFormMap damagePartFormMap=getFormMap(DamagePartFormMap.class);
		//限定status为1
		damagePartFormMap.put("status", 1);
		List<Map<String, Object>> part = damagePartMapper.getAll(damagePartFormMap);
		LuogicPaging(pageNow, pageSize, part);
		pageView.setRecords(part);
		return pageView;
	}
	
	@RequestMapping("damagePart_edit")
	public String editUI(Model model) throws Exception 
	{
		String id = getPara("id");
		if(Common.isNotEmpty(id)){
			model.addAttribute("damagePart",baseMapper.findbyFrist("id", id, DamagePartFormMap.class));
		}
		return Common.BACKGROUND_PATH + "/mould/damagePart/damagePart_edit";
	}
	
	@ResponseBody
	@RequestMapping(value="damagePart_editEntity")
	@SystemLog(module="模具易损件统计表",methods="编辑模具易损件统计表-修改模具易损件统计表")//凡需要处理业务逻辑的.都需要记录操作日志
	public String editEntity() throws Exception {
		DamagePartFormMap damagePartFormMap = getFormMap(DamagePartFormMap.class);
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date=new Date();
		damagePartFormMap.put("date", sdf.format(date));
		// 获取request
		HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();  
		// 通过工具类获取当前登录的bean
		UserFormMap userFormMap = (UserFormMap)Common.findUserSession(request);
		damagePartFormMap.put("fillPeopleId", userFormMap.getInt("id"));
		damagePartMapper.editEntity(damagePartFormMap);
		return "success";
	}
	
	@RequestMapping("damagePart_add")
	public String addUI(Model model) throws Exception {
		return Common.BACKGROUND_PATH + "/mould/damagePart/damagePart_add";
	}
	
	@ResponseBody
	@RequestMapping("damagePart_addEntity")
	@Transactional(readOnly=false)//需要事务操作必须加入此注解
	@SystemLog(module="模具易损件统计表",methods="编辑模具易损件统计表-新增模具易损件统计表")//凡需要处理业务逻辑的.都需要记录操作日志
	public String addEntity() throws Exception {
		DamagePartFormMap damagePartFormMap = getFormMap(DamagePartFormMap.class);
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date=new Date();
		damagePartFormMap.put("date", sdf.format(date));
		// 获取request
		HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();  
		// 通过工具类获取当前登录的bean
		UserFormMap userFormMap = (UserFormMap)Common.findUserSession(request);
		damagePartFormMap.put("fillPeopleId", userFormMap.getInt("id"));
		damagePartMapper.addEntity(damagePartFormMap);
		return "success";
	}
	
	@ResponseBody
	@RequestMapping("damagePart_deleteEntity")
	@Transactional(readOnly=false)//需要事务操作必须加入此注解
	@SystemLog(module="模具易损件统计表",methods="编辑模具易损件统计表-删除模具易损件统计表")//凡需要处理业务逻辑的.都需要记录操作日志
	public String deleteEntity() throws Exception {
		DamagePartFormMap damagePartFormMap=getFormMap(DamagePartFormMap.class);
		String[] ids = getParaValues("ids");
		String arr=ids[0];
		String[] str=arr.split(",");
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date=new Date();
		// 获取request
		HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();  
		// 通过工具类获取当前登录的bean
		UserFormMap userFormMap = (UserFormMap)Common.findUserSession(request);
		for(int i=0;i<str.length;i++){
			damagePartFormMap.put("date", sdf.format(date));
			damagePartFormMap.put("fillPeopleId", userFormMap.getInt("id"));
			damagePartFormMap.put("status", 0);
			damagePartFormMap.put("id", str[i]);
			damagePartMapper.editEntity(damagePartFormMap);
		}
		return "success";
	}
}













